from core import LOUTI, JIAOHU, MESS, MIXED, ZONGLIAN, File
from core import LEFT, RIGHT, RANDOM, BITE
from core import Length, Key
import core
from game import runGame
import colorama
import json
import random
import traceback


colorama.init()


def freeze():
    data = {}
    data["CODE"] = str(core.CODE)
    data["KEY"] = core.KEY.value
    data["LENGTH"] = core.LENGTH.value
    data["ALPHA"] = core.ALPHA
    data["TIMER"] = str(core.TIMER)
    path = f"config{random.randint(10000, 99999)}.ed"
    with open(path, "w") as f:
        json.dump(data, f)
    print(f"{colorama.Fore.YELLOW}File saved at .\\{path}{colorama.Fore.RESET}")


def load(path):
    with open(path, "r") as f:
        data = json.load(f)
    core.CODE = []
    for code in eval(data["CODE"]):
        core.CODE.append(core.CONST_OBJECT(code, ''))
    core.KEY = core.Key(data["KEY"])
    core.LENGTH = core.Length(data["LENGTH"])
    core.ALPHA = data["ALPHA"]
    core.TIMER = eval(data["TIMER"])
    print(f"{colorama.Fore.GREEN}Succeed.{colorama.Fore.RESET}")

# result, status =
# print(result)
# if status:
#     game.runGame()


print("Hello from eatNote. See source code at https://gitee.com/xi-to-porridge/eat-note")
while (string := input(">>> ")) != "/exit":
    try:
        _ = eval(string)
        if _ is not None:
            print(_)
    except SyntaxError:
        try:
            exec(string)
        except Exception as e:
            print(f"{colorama.Fore.RED}[Exception]{colorama.Fore.RESET}", e)
    except Exception as e:
        # print(traceback.format_exc())
        print(f"{colorama.Fore.RED}[Exception]{colorama.Fore.RESET}", e)


# TODO: 将死亡判定与时间判定分开
